﻿/* =============================================
 * 名    称：MessageHandler
 * 功能描述：MessageHandler
 * 作    者：yanwei.yang
 * 添加时间：2015/4/15 16:56:08
 * =============================================*/


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml;
namespace Summer.WeiXin.Core
{
    public class MessageService
    {
        /// <summary>
        /// 获取用户发送过来的微信信息
        /// </summary>
        /// <returns></returns>
        public static Message GetMessage()
        {
            Message wx = new Message();
            StreamReader sr = new StreamReader(HttpContext.Current.Request.InputStream, Encoding.UTF8);
            XmlDocument xml = new XmlDocument();
            xml.Load(sr);
            wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;
            wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;
            wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;
            wx.MsgId = xml.SelectSingleNode("xml").SelectSingleNode("MsgId").InnerText;
            wx.CreateTime = xml.SelectSingleNode("xml").SelectSingleNode("CreateTime").InnerText;
            if (wx.MsgType.Trim() == "text")
            {
                wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText;
            }
            if (wx.MsgType.Trim() == "event")
            {
                wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;
            }
            return wx;
        }


    }
}
